var S1MModal = (function($) {
  _BUTTON_OK = 0;
  _BUTTON_YES = 1;
  _BUTTON_NO = 2;

  function _alert(title, message, buttonText, callback) {
    $('#alertModal .modal-title').html(title);
    $('#alertModal .modal-message').html(message);
    if(typeof buttonText == "undefined" || buttonText == null) {
      buttonText = resources[IResourceName.CLOSE];
    }
    $('#alertButton').text(buttonText);
    $('#alertModal .button-ok').off('click').on('click', function() {
      var event = {
        button : _BUTTON_OK
      };
      if (callback) {
        callback(event);
      }
      $('#alertModal').modal('hide');
      return false;
    });
    
    // S1M-7458 / S1M-8011
    $('#alertModal').on('hidden', function () {  
    	var lastFocused = event.target;
    	
    	var elementForFocus = $('#AuthInfoDataModal #AUTHOR_DEPARTMENT_0');
    	if(elementForFocus != undefined){
    		elementForFocus.focus();
    	}  	    	 
    	// S1M-8061
    	elementForFocus = $('#InfoModal #DEPARTMENT');
    	if(elementForFocus != undefined){
    		elementForFocus.focus();
    	}  	    	    	
    	if(elementForFocus == undefined && lastFocused != undefined){
    		lastFocused.focus();
    	}
    });
    
    $('#alertModal #alertButton').on('keypress', function() {        		
		$('#alertModal').modal('hide');        		
    });
    
    $('#alertModal').modal('show');
    return $('#alertModal');
  }
  
  //SF_23821
  function _confirm(title, message, noButtonText, yesButtonText, callback, dismissPrevModal, prevModalName) {
    $('#confirmationModal .modal-title').html(title);
    $('#confirmationModal .modal-message').html('<strong>' + message +'</strong>');
    if(dismissPrevModal==true){
    	$('#confirmationModal .modal-footer .button-no').attr('href',prevModalName);
    	$('#confirmationModal .modal-footer .button-no').attr('data-toggle','modal');
    	$('#confirmationModal .modal-footer .button-no').attr('data-dismiss','modal');
    	$('#confirmationModal .modal-footer .button-yes').attr('href',prevModalName);
    	$('#confirmationModal .modal-footer .button-yes').attr('data-toggle','modal');
    	$('#confirmationModal .modal-footer .button-yes').attr('data-dismiss','modal');
    }
    var processed=false;
    if (typeof noButtonText == "undefined" || noButtonText == null) {
      $('#confirmationModal .modal-no-close').html(resources[IResourceName.NO_CLOSE]);
    } else {
      $('#confirmationModal .modal-no-close').html(noButtonText);
    }

    if (typeof yesButtonText == "undefined" || yesButtonText == null) {
      $('#confirmationModal .modal-yes').html(resources[IResourceName.YES]);
    } else {
      $('#confirmationModal .modal-yes').html(yesButtonText);
    }

    $('#confirmationModal .button-no').off('click').on('click',function() {
      var event = {
        button : _BUTTON_NO
      };
      callback(event);
      processed=true;
      $('#confirmationModal').modal('hide');
      if(dismissPrevModal ){
    	  $(prevModalName).modal('show');
      }
      return false;
    });
    
    $('#confirmationModal .button-close').off('click').on('click',function() {
      var event = {
    	  button : (callback.revertedLogic!='undefined' && callback.revertedLogic)? _BUTTON_YES : _BUTTON_NO
      };
      callback(event);
      processed=true;
      $('#confirmationModal').modal('hide');
    
    	  $('#confirmationModal').modal('hide');
      return false;
    });

    $('#confirmationModal .button-yes').off('click').on('click',function() {
      var event = {
        button : _BUTTON_YES
      };
      callback(event);
      processed=true;
      $('#confirmationModal').modal('hide');
      if($('#confirmationModal .modal-footer .button-yes').attr('href')!=null){
    	  var modalName = $('#confirmationModal .modal-footer .button-yes').attr('href');
    	  $(modalName).modal('show');
      }
      return false;
    });

    $('#confirmationModal').off('hide').on('hide',function() {
      if (!processed) {
        var event = {
        	button : (callback.revertedLogic!='undefined' && callback.revertedLogic)? _BUTTON_YES : _BUTTON_NO
        };
        callback(event);
      }
      return true;
    });

    $('#confirmationModal').modal('show');
    return $('#confirmationModal');
  }

  function _hideProgress() {
    resetFileUploadModalAndStatusCheckTimerFl = true; //SF-29469 and 28473 stop infinite polling forcibly under race condition.  
    S1MFileUploadProgressUtils.resetFileUploadModalAndStatusCheckTimer();
  }

  function _showProgress(title, message, warningText, progressText) {
    $('#progressModal .modal-title').html(title);
    $('#progressModal .modal-message').html(message);
    $('#progressModal .modal-progress-text').html(progressText);
    $('#progressModal .modal-progress-warning').html(warningText);
    $('#progressModal').modal('show');
  }
  
  function _showProgressBarIndicator(title, message, warningText, progressText, cancelButtonText) {
	  //cancel button
	  $('#progressModal #cancelProgress').attr('style', "outline: medium none;");
	  $('#progressModal #cancelProgress').text(cancelButtonText);
	  $('#progressModal #cancelProgress').attr('data-dismiss','modal');
	  //progress bar indicator
	  $('#progressModal .progress .bar').attr('style', "width:0%;");
	  $('#progressModal .progress').modal('show');
	  $('#progressModal .loadingbar').hide();
	  _showProgress(title, message, warningText, progressText);
  }

  function _showFileUploadProgress(title, message, warningText, progressText, callback) {
    _showProgress(title, message, warningText, progressText);

    //create the observer and observable
    S1MFileUploadProgressUtils.prepareToObserveFileUpload();
  }

  return {
    alert : _alert,
    confirm : _confirm,
    hideProgress : _hideProgress,
    showFileUploadProgress : _showFileUploadProgress,
    showProgress : _showProgress,
    showProgressBarIndicator : _showProgressBarIndicator,
  };
})(jQuery);
